package Chapter1.link;

import leetcode.ListNode;

public class RemoveLinkNodeDemo {

    public static void main(String[] args) {
        leetcode.ListNode listNode = leetcode.ListNode.createByArray(new int[]{1, 2, 3, 4, 5});
        leetcode.ListNode.print(removeNode(listNode, 2));
    }

    public static ListNode removeNode(ListNode head, int n) {
        ListNode p = new ListNode(-1);
        p.next = head;
        ListNode fast = p;
        ListNode slow = p;
        for (int i = 0; i <= n; i++) {
            fast = fast.next;
        }
        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return p.next;
    }
}
